package org.code.boot.controller.boot;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.Map;
import java.util.Collections;
import cn.hutool.core.collection.CollUtil;
import org.code.boot.common.utils.CollectionUtils;
import org.code.boot.common.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.domain.boot.BizOrderHistory;
import org.code.boot.service.boot.IBizOrderHistoryService;
import org.code.boot.vo.TableDataInfo;
import org.code.boot.domain.system.SysUser;
import org.code.boot.service.system.ISysUserService;
import org.code.boot.domain.boot.BizTrainOrder;
import org.code.boot.service.boot.IBizTrainOrderService;

/**
 * 订单历史记录Controller
 * 
 * @author coder
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/boot/history")
public class BizOrderHistoryController extends BaseController
{
    @Autowired
    private IBizOrderHistoryService bizOrderHistoryService;

    @Autowired
    private ISysUserService sysuserService;

    @Autowired
    private IBizTrainOrderService biztrainorderService;

    /**
     * 查询订单历史记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizOrderHistory bizOrderHistory)
    {
        startPage();
        List<BizOrderHistory> list = bizOrderHistoryService.selectBizOrderHistoryList(bizOrderHistory);
        list = buildDetailList(list);
        return getDataTable(list);
    }



    /**
     * 获取订单历史记录下拉选项列表
     */
    @GetMapping("/options")
    public AjaxResult getOptions()
    {
        List<BizOrderHistory> list = bizOrderHistoryService.selectBizOrderHistoryList(new BizOrderHistory());
        return success(list);
    }

    /**
     * 获取订单历史记录详细信息
     */
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId)
    {
        return success(bizOrderHistoryService.selectBizOrderHistoryByHistoryId(historyId));
    }

    /**
     * 新增订单历史记录
     */
    @Log(title = "订单历史记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizOrderHistory bizOrderHistory)
    {
        bizOrderHistory.setCreateBy(getNickname());
        return toAjax(bizOrderHistoryService.insertBizOrderHistory(bizOrderHistory));
    }

    /**
     * 修改订单历史记录
     */
    @Log(title = "订单历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizOrderHistory bizOrderHistory)
    {
        bizOrderHistory.setUpdateBy(getNickname());
        return toAjax(bizOrderHistoryService.updateBizOrderHistory(bizOrderHistory));
    }

    /**
     * 删除订单历史记录
     */
    @Log(title = "订单历史记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(bizOrderHistoryService.deleteBizOrderHistoryByHistoryIds(historyIds));
    }

    /**
     * 构建详细信息列表
     * 
     * @param list 列表数据
     * @return 构建后的列表数据
     */
    private List<BizOrderHistory> buildDetailList(List<BizOrderHistory> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        
        // 获取用户Map
        Map<Long, SysUser> userIdMap = sysuserService.getSysUserMap(
                CollectionUtils.convertSet(list, BizOrderHistory::getUserId));
        
        // 获取关联订单Map
        Map<Long, BizTrainOrder> orderIdMap = biztrainorderService.getBizTrainOrderMap(
                CollectionUtils.convertSet(list, BizOrderHistory::getOrderId));
        
        // 拼接结果
        list.forEach(item -> {
            // 拼接用户名称
            MapUtils.findAndThen(userIdMap, item.getUserId(), 
                userIdItem -> item.setUserIdName(userIdItem.getNickName()));
            // 拼接关联订单名称
            MapUtils.findAndThen(orderIdMap, item.getOrderId(), 
                orderIdItem -> item.setOrderIdName(orderIdItem.getPassengerName()));
        });
        
        return list;
    }
}
